---------------------------------------
--            AmBScrollBar           --
---------------------------------------
local ScrollBar = {
   Init = function(self)
      self:SetMouseInputEnabled(true)

      local main = vgui.Create("AmBPaddedPanel", self)
      main:SetDocking(true)
      self._MainPanel = main
      local sub = main:GetInterior()
      self._SubPanel = sub

      local slider = vgui.Create("AmBPaddedPanel", sub)
      self._SliderPanel = slider

      --Slider functions
      sub.PanelOnMouse = function(sub, Type, Code, X, Y)
         local height = sub:GetTall()
         local rloc = Y / height
         local aloc = rloc * self._FullSize
         local sloc = aloc - (self._ScrollSize / 2)
         if Type == 1 and Code == 107 then
            self:SetScrollPosition(sloc)
            sub._MouseUsed = true
            return true
         end
         if Type == 2 and sub._MouseUsed then
            self:SetScrollPosition(sloc)
            return true
         end
         if Type == 3 and Code == 107 and sub._MouseUsed then
            self:SetScrollPosition(sloc)
            sub._MouseUsed = false
            return true
         end
      end

      self:SetColor(ThemeVar("default_scrollbar_fore_color") or Color(255,255,255,255), ThemeVar("default_scrollbar_back_color") or Color(0,0,0,255))

      self._ScrollSize = 0.1 -- Size shown at any time
      self._FullSize = 1.0 -- Size that can every be shown
      self._ScrollPos = 0.0 -- Top of the scrollbar thing
		self._TargetWidth = ThemeVar("default_scrollbar_width") or 20 -- What I want my width to be.

      self._SmallestSliderSize = ThemeVar("default_scrollbar_slider_minsize") or 12 -- Smallest the slider can be. (Will explode if made smaller)
   end,

   -- Sets the fore and back color of the scrollbar
   SetColor = function(self, Fore, Back)
      self._MainPanel:SetPaddingColor(Fore, Back)
      self._SubPanel:SetPanelColor(Fore)
      self._SliderPanel:SetPaddingColor(Back, Fore)
      self._SliderPanel:GetInterior():SetPanelColor(Back)
   end,

   -- Sets the scrollsize and fullsize of the scrollbar.
   SetScrollValue = function(self, ScrollSize, FullSize)
      self._ScrollSize = ScrollSize
      self._FullSize = FullSize
      if self._FullSize <= 0 then
         self._FullSize = 1
         self._ScrollSize = 1
      end
      if self._ScrollSize > self._FullSize then
         self._ScrollSize = self._FullSize
      end
      self:SetScrollPosition(self._ScrollPos)
      self:_UpdateSlider()
   end,

   -- Sets the position of the scrollbar (And calls onvaluechanged).
   SetScrollPosition = function(self, NewPosition)
      NewPosition = math.max(0, NewPosition)
      NewPosition = math.min(NewPosition, self._FullSize - self._ScrollSize) --Keep slider in bounds.
      if self._ScrollPos != NewPosition then
         self.OnValueChanged(NewPosition)
         self._ScrollPos = NewPosition
         self:_UpdateSlider()
      end
   end,

   -- Returns the scrollsize and fullsize of the scrollbar.
   GetScrollValue = function(self)
      return self._ScrollSize, self._FullSize
   end,
   -- Gets the position of the slider
   GetScrollPosition = function(self)
      return self._ScrollPos
   end,

   -- Callback
   OnValueChanged = function(NewValue)
      
   end,

   PanelThink = function(self)
      self:_UpdateSlider()
		self:RequestResize(Freeze(self._TargetWidth), self:GetTall())
   end,

   _UpdateSlider = function(self)
      self._ScrollPos = math.max(0, self._ScrollPos)
      self._ScrollPos = math.min(self._ScrollPos, self._FullSize - self._ScrollSize) --Keep slider in bounds.
      local width = self._SubPanel:GetWide()
      local height = self._SubPanel:GetTall()
      local sliheight = height * self._ScrollSize / self._FullSize
      sliheight = math.max(sliheight, Freeze(self._SmallestSliderSize))
      local sliy = self._ScrollPos / self._FullSize * height
      self._SliderPanel:SetPos(0, sliy)
      self._SliderPanel:SetSize(width, sliheight)
   end
}
AmB.UI.ScrollBar = ScrollBar
vgui.Register("AmBScrollBar", ScrollBar, "AmBContentPanel")